package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.dao.OperatorDao;
import com.dlc.felear.xiaoensale.deviceControlSf.OperatorAutoHandle;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.Session;
import com.dlc.felear.xiaoensale.utils.MenuPicUtil;
import com.itdlc.android.library.base.BaseActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 菜谱信息显示页面
 */
public class EmnuInfoActivity extends BaseActivity implements SingleInputDialog.SingleDialogCallback {

    private static final String TAG = "EmnuInfoActivity";
    @BindView(R.id.e_iv_pic)
    ImageView pic;
    @BindView(R.id.e_tv_name)
    TextView etName;
    @BindView(R.id.e_tv_pname)
    TextView etPName;
    @BindView(R.id.e_tv_duration)
    TextView etDuration;
    @BindView(R.id.e_tv_size)
    TextView etSize;
    private CookingMenu cookingMenu;
    private TopbarHelper topbarHelper;
    private CookingMenuDao cookingMenuDao;
    private OperatorDao operatorDao;
    private SingleInputDialog singleInputDialog;
    //    private String id;
    private String type = "M400";
    private float minWeight = 0;
    private float maxWeight = 0;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_emnu_info;
    }

    @Override
    protected void initialView() {
        try {
            cookingMenuDao = new CookingMenuDao(this);
            topbarHelper = new TopbarHelper(this);
            topbarHelper.setcon(R.drawable.arrow_left);
            topbarHelper.setNavActivity(CookingMenuActivity.class);
            topbarHelper.setTitle("菜谱简介");
            operatorDao = new OperatorDao(this);
            singleInputDialog = new SingleInputDialog(this, this);
            singleInputDialog.setEtInputType(InputType.TYPE_CLASS_NUMBER);
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
        }
    }

    @Override
    protected void initData() {
        try {
            type = Session.getSession().getString(Constant.D_GT_TYPE, "M400");
            float[] temp = getLimitValue();
            minWeight = temp[0];
            maxWeight = temp[1];
        } catch (Exception e) {
            type = "M400";
            showShortToast("数据异常:" + e.getMessage());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            String id = getIntent().getStringExtra("id");
            if (!TextUtils.isEmpty(id) || cookingMenuDao == null) {
                initCookMenu(cookingMenuDao.queryById(id));
            } else {
                showLongToast("数据异常");
                finish();
            }
        } catch (Exception e) {
            showLongToast("数据异常");
            finish();
        }
    }

    private void initCookMenu(CookingMenu cookingMenu) {
        try {
            if (cookingMenu != null) {
                this.cookingMenu = cookingMenu;
                etName.setText(cookingMenu.getName());
                ccTime(cookingMenu.getId());
                etSize.setText(cookingMenu.getSpecification() + " 斤");
                Glide.with(this).load(MenuPicUtil.getInstance(this).getLoaclpicPath(cookingMenu.getPic(), cookingMenu.getName())).into(pic);
            } else {
                showLongToast("炒菜菜谱不存在");
                finish();
            }
        } catch (Exception e) {
            showShortToast("数据异常:" + e.getMessage());
            finish();
        }
    }

    private void ccTime(String id) {
        try {
            int temp = 0;
            List<Operator> tempList = operatorDao.queryForAllByMID(id);
            for (Operator operator : tempList) {
                temp += operator.getDelayTime();
                Log.e(TAG, " 当前步骤时长：" + operator.getDelayTime() + " 累计时长:" + temp);
            }
            int tempDuring = OperatorAutoHandle.getInstance(null, null, "EmnuInfoActivity")
                    .getOperDuring(tempList);
            etDuration.setText(temp + "秒");
            Log.e(TAG, "tempDuring=" + (tempDuring) / 1000 + " temp=" + temp);
        } catch (Exception e) {
            showLongToast("数据异常:" + e.getMessage());
        }
    }


    @OnClick({R.id.e_tv_cpwt, R.id.e_tv_kscc, R.id.e_iv_select})
    public void clickView(View view) {
        try {
            switch (view.getId()) {
                case R.id.e_tv_cpwt:
                    if (cookingMenu == null) {
                        showLongToast("菜谱信息为空，请退回菜谱界面重新选择菜谱");
                        return;
                    }
                    Intent intent = new Intent(this, MiroEditMenuActivity.class);
                    intent.putExtra("cookingMenu", cookingMenu);
                    intent.putExtra("menuId", cookingMenu.getId());
                    intent.putExtra("src", 0);
                    startActivity(intent);
                    finish();
                    break;
                case R.id.e_tv_kscc:
                    if (cookingMenu == null) {
                        showLongToast("菜谱信息为空，请退回菜谱界面重新选择菜谱");
                        return;
                    }
                    singleInputDialog.setHintMsg("请输入炒菜重量（≥4）斤");
                    singleInputDialog.setTips("炒菜斤数");
                    singleInputDialog.showDialog(R.id.e_tv_kscc);
                    break;
            }
        } catch (Exception e) {
            showLongToast("数据异常:" + e.getMessage());
        }
    }

    private void gotoOtherActivity(float size) {
        try {
            Intent intent1 = new Intent(this, CookingActivity.class);
            cookingMenu.setSpecification(Float.toString(size));
            intent1.putExtra("cookingMenu", JsonHelper.toJson(cookingMenu));
            intent1.putExtra("operators", "operators");
            startActivity(intent1);
            finish();
        } catch (Exception e) {
            showLongToast("数据异常:" + e.getMessage());
        }

    }


    @Override
    public void SingleDialogInputResult(String value, int id) {
        try {
            if (id == R.id.e_tv_kscc) {
                float weight = Float.parseFloat(value);
                if (weight < minWeight) {
                    showLongToast("不能小于" + minWeight + "斤");
                    return;
                }
                if (weight > maxWeight) {
                    showLongToast("不能大于" + maxWeight + "斤");
                    return;
                }
                if (TextUtils.isEmpty(cookingMenu.getParam1())) {
                    showLongToast("请先到微调界面设置参数");
                    return;
                }
                gotoOtherActivity(weight);
            }
        } catch (Exception e) {
            showLongToast("请输入合法的数值:" + e.getMessage());
        }
    }

    private float[] getLimitValue() {
        float[] temp = new float[2];
        if (TextUtils.equals(type, "M600")) {
            temp[0] = 5;
            temp[1] = 80;
        }
        if (TextUtils.equals(type, "M400")) {
            temp[0] = 4;
            temp[1] = 40;
        }
        if (TextUtils.equals(type, "M260")) {
            temp[0] = 0.5f;
            temp[1] = 7f;
        }
        return temp;
    }
}